#include "MenuButton.h"
#include "InputHangdler.h"
#include "Point.h"
#include "Game.h"

MenuButton::MenuButton(void(*function)() , Sprite &_sprite_button ,const int &_idSprite, SDL_Renderer* pRenderer)
: m_function(function)
{
	m_sprite_menu_button = _sprite_button;
	m_idSprite = _idSprite;
	m_pRendere = pRenderer;
	m_bButtonDown = false;
	TheTextureManager::Instance()->add(m_sprite_menu_button, m_pRendere);
	m_bReleased = true;
}

MenuButton::~MenuButton()
{
	// do nothing
}

void MenuButton::update()
{
	//TheInputHangdler::Instance()->update();
	
	m_infoButton.x = m_sprite_menu_button.getSpriteImage()[m_idSprite].image_possition_x;
	m_infoButton.y = m_sprite_menu_button.getSpriteImage()[m_idSprite].image_possition_y;
	m_infoButton.width = m_sprite_menu_button.getSpriteImage()[m_idSprite].image_possition_width;
	m_infoButton.height = m_sprite_menu_button.getSpriteImage()[m_idSprite].image_possition_height;

	Point move;
	move.x = TheInputHangdler::Instance()->getMousePossitionMove().x;
	move.y = TheInputHangdler::Instance()->getMousePossitionMove().y;

	if (move.x >= m_infoButton.x && move.x <= m_infoButton.x + m_infoButton.width
		&& move.y >= m_infoButton.y && move.y <= m_infoButton.y + m_infoButton.height
		&& moveMouse == true)
	{
		m_idSprite += 1;
		moveMouse = false;
	}
	if ((move.x <= m_infoButton.x || move.x >= m_infoButton.x + m_infoButton.width 
		|| move.y <= m_infoButton.y || move.y >= m_infoButton.y + m_infoButton.height) && moveMouse == false)
	{
		moveMouse = true;
		m_idSprite -= 1;
	}

	Point cliked;
	cliked.x = TheInputHangdler::Instance()->getMousePossition().x;
	cliked.y = TheInputHangdler::Instance()->getMousePossition().y;

	if (cliked.x >= m_infoButton.x && cliked.x <= m_infoButton.x + m_infoButton.width
		&& cliked.y >= m_infoButton.y && cliked.y <= m_infoButton.y + m_infoButton.height
		&& TheInputHangdler::Instance()->getResetMouseDown() == true)
	{
		m_function();
		int iFalse = 1;
		TheInputHangdler::Instance()->resetMouseDown(iFalse);
	}
}

void MenuButton::draw()
{

	TheTextureManager::Instance()->drawParallax(m_sprite_menu_button, m_idSprite, m_pRendere);
	//GameObject::draw();
}

void MenuButton::clean()
{
	GameObject::clean();
}

void MenuButton::getButtonDown()
{
	m_bButtonDown = false;
}